<?php
/***************************************************************************
*                                register.php
 *
 *   Begin date           	: 
 *   Copyright            	: 
 *   Email                	: 
 ***************************************************************************/
defined('PHP5_PHP') or die("Application is stopping!!!");
if(!checkLoginUser()) {
	$msgAlert = $lang['_LOGIN_MSG_'];
	$redirect = sefBuild($php5WebPath, '', 1);
}
require_once($php5RootPath . "/classes/message_sms.class.php");
require_once($php5RootPath . "/classes/message_sms_detail.class.php");
$row = new SMSMessage($php5DB);

$task 		= php5GetParam($_REQUEST, 'task', '');
$smarty->setCompileDir(sprintf($php5RootTempalteCPath, ''));
$tpl = sprintf($php5TemplateFile, $language, 'sms', 'create.tpl');
if(!is_file($tpl )) {
	die("can not find template");
}
//list group
$where = array();
$groups = array();
$groups[0] = "None";
$where[] = " user_id = '".$php5Session->getVar("user_id")."' ";
$rows_temp = array();
$query = "SELECT id, name "
. "\n FROM #__newsletter_user_group "
. ( count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "" )
. "\n ORDER BY id";
$php5DB->setQuery( $query );
$rows_group =  $php5DB->loadObjectList();
foreach($rows_group as $k => $row_temp) {
	$groups[$row_temp->id] = $row_temp->name;
}

//check task
$msg = '';
$noerr = 1;
switch($task)
{
	case 'add':
		$row->bind( $_POST );
		if(!trim($row->phone)) {
			$noerr = 0;
			$msg = $msgAlert = sprintf($lang['_BLANK_MSG_'], 'Phone');
			break;
		}	
		if( preg_match("/^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/", $row->phone )==false) {
			$noerr = 0;
			$msg = $msgAlert = sprintf($lang['_INVALID_MSG_'], 'Phone');
			break;
		}			
		if(!trim($row->message)) {
			$noerr = 0;
			$msg = $msgAlert = sprintf($lang['_BLANK_MSG_'], 'Message');
			break;
		}
		if(strlen($row->message) > 124) {
			$noerr = 0;
			$msg = $msgAlert = "Please input message length <= 124";
			break;
		}
		if($noerr) {
			$row->id = 0;			
			$row->user_id = $php5Session->getVar("user_id");
			if (!$row->store()) {
				$msg = $msgAlert = "Error!!! Please try again!!!";		
			} else {
				$msg = $msgAlert = $lang['_UPDATE_MSG_'];				
				$redirect = sefBuild($php5WebPath, 'index.php?o=sms&m=main', 1);
			}
			
		}
		
		break;
		default:

		break;	
		
}
$smarty->assign('msg', '');
if($msg) {
	$smarty->assign('msg', $msg);
}
//assign

$smarty->assign('groups', $groups);
$smarty->assign('id', $row->id);
$smarty->assign('message', $row->message);
$smarty->assign('phone', $row->phone);
$smarty->assign('group_id', $row->group_id);
$smarty->assign('date', $row->date);

$smarty->assign('link_return', sefBuild($php5WebPath, 'index.php?o=sms&m=main', 1));
$smarty->assign('action', sefBuild($php5WebPath, 'index.php?o=sms&m=create&task=add', 1));
$smarty->assign('main', $smarty->fetch($tpl));

//$smarty->display($tpl);

return;
